﻿using System;

namespace OneWorks.Common
{
    public static class RandomHandle
    {
        /// <summary>
        /// 随机字母与数字
        /// </summary>
        /// <returns></returns>
        public static string RandomConfusion(int length)
        {
            string randomcode = String.Empty;
            Random random = new Random();//为了得到不同的随机序列
            //生成一定长度的验证码             
            for (int i = 0; i < length; i++)
            {
                int rand = random.Next(9);
                char code;
                if (rand % 3 == 0)
                {
                    code = (char)('A' + (char)(rand % 26));
                }
                else
                {
                    code = (char)('0' + (char)(rand % 10));
                }
                randomcode += code.ToString();
            }
            return randomcode;
        }

        ///  <summary>   
        /// 随机数字
        /// </summary>
        /// <returns></returns>
        public static string RandomNumbers(int length)
        {
            string code = string.Empty;
            Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                code = code + random.Next(9);
            }
            return code;
        }

        ///  <summary>   
        /// 随机字母
        /// </summary>
        /// <returns></returns>
        public static string RandomLetters(int length)
        {
            const string vchar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
            string[] vcArray = vchar.Split(',');
            string checkCode = string.Empty;
            for (int i = 0; i < length; i++)
            {
                Random random = new Random(unchecked((int)DateTime.Now.Ticks));//为了得到不同的随机序列
                int t = random.Next(vcArray.Length);// The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero，下标从0开始
                checkCode += vcArray[t];
            }
            return checkCode;
        }

        /// <summary>
        /// 随机中文码
        /// </summary>
        /// <returns></returns>
        public static string RandomChinese(int length)
        {
            System.Text.Encoding gb = System.Text.Encoding.Default;//获取GB2312编码页（表）
            object[] bytes = RandomChineseCode(length);//调用函数产生4个随机中文汉字编码
            string[] str = new string[4];
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < 4; i++)
            {
                //根据汉字编码的字节数组解码出中文汉字
                str[i] = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
                sb.Append(str[i]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 产生随机中文汉字编码
        /// </summary>
        /// <param name="strlength"></param>
        /// <returns></returns>
        private static object[] RandomChineseCode(int strlength)
        {
            //定义一个字符串数组储存汉字编码的组成元素
            string[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
            Random rnd = new Random();
            object[] bytes = new object[strlength];

            for (int i = 0; i < strlength; i++)
            {
                //区位码第1位
                int r1 = rnd.Next(11, 14);
                string strR1 = rBase[r1].Trim();

                //区位码第2位
                rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);
                int r2 = rnd.Next(0, r1 == 13 ? 7 : 16);
                string strR2 = rBase[r2].Trim();

                //区位码第3位
                rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机种子
                int r3 = rnd.Next(10, 16);
                string strR3 = rBase[r3].Trim();

                //区位码第4位
                rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
                int r4;
                switch (r3)
                {
                    case 10:
                        r4 = rnd.Next(1, 16);
                        break;
                    case 15:
                        r4 = rnd.Next(0, 15);
                        break;
                    default:
                        r4 = rnd.Next(0, 16);
                        break;
                }
                string strR4 = rBase[r4].Trim();

                //定义两个字节变量存储产生的随机汉字区位码
                byte byte1 = Convert.ToByte(strR1 + strR2, 16);
                byte byte2 = Convert.ToByte(strR3 + strR4, 16);

                //将两个字节变量存储在字节数组中
                byte[] strR = { byte1, byte2 };

                //将产生的一个汉字的字节数组放入object数组中
                bytes.SetValue(strR, i);
            }
            return bytes;
        }
    }
}
